package com.library.service;

import com.library.bean.ReaderCard;
import com.library.entity.OaDepartmentUserLinkExt;
import com.library.entity.UserInfo;
import com.library.controller.RCException;
import com.library.entity.OaDepartment;
import com.library.mapper.OaDepartmentMapper;
import com.library.mapper.OaDepartmentUserLinkMapper;
import com.library.mapper.UserInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ido
 * @since 2024-12-14
 */

@Service
public class OaDepartmentServiceImpl
//        extends ServiceImpl<OaDepartmentMapper, OaDepartment>
//        implements IOaDepartmentService
{
    private final static Logger log = LoggerFactory.getLogger(LoginService.class);

    @Autowired
    OaDepartmentMapper oaDepartmentMapper;

    @Autowired
    OaDepartmentUserLinkMapper oaDepartmentUserLinkMapper;

    public List<OaDepartment> list(OaDepartment oaDepartment){
        List<OaDepartment> list = oaDepartmentMapper.listByQuery(oaDepartment);
//        for (OaDepartment department : list) {
//            department.setRole(CommonUtils.getLoginReaderCardRole());
//        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(OaDepartment oaDepartment, HttpServletRequest request1) {
        ReaderCard loginReaderCard = CommonUtils.getLoginReaderCard();
        OaDepartment query = new OaDepartment();
        query.setName(oaDepartment.getName());
        List<OaDepartment> oaDepartments = this.oaDepartmentMapper.listByQuery(query);
        if(!oaDepartments.isEmpty()){
            throw new RCException("部门名称重复了");
        }
        oaDepartmentMapper.add(oaDepartment);
    }

    public void delete(OaDepartment oaDepartment) {
        OaDepartmentUserLinkExt query = new OaDepartmentUserLinkExt();
        query.setOaDepartmentId(oaDepartment.getId());
        List<OaDepartmentUserLinkExt> oaDepartmentUserLinkExts = this.oaDepartmentUserLinkMapper.listByQuery(query);
        if(!oaDepartmentUserLinkExts.isEmpty()){
            throw new RCException("该部门下有员工，不能删除");
        }
        this.oaDepartmentMapper.deleteByIdNotNull(oaDepartment);
    }

    @Autowired
    UserInfoMapper userInfoMapper;

    public OaDepartment detail(OaDepartment oaDepartment) {
        OaDepartment oaDepartment1 = this.oaDepartmentMapper.selectByPrimaryKey(oaDepartment.getId());
        UserInfo userInfo = this.userInfoMapper.selectByPrimaryKey(oaDepartment1.getManagerUserInfoId());
        oaDepartment1.setUserName(Optional.ofNullable(userInfo).map(e->e.getName()).orElse(""));
        return oaDepartment1;
    }

    public void update(OaDepartment oaDepartment) {
        int i = this.oaDepartmentMapper.updateByPrimaryKeySelective(oaDepartment);
    }
}
